//
//  MSBaseShowView.m
//  MSJD
//
//  Created by xiangbo on 2018/9/29.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSBaseTablePageView.h"
#import "MSBaseShowCell.h"

@interface MSBaseTablePageView ()

@property (assign, nonatomic) UITableViewStyle   style;

@end

@implementation MSBaseTablePageView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.style = UITableViewStylePlain;
        [self setupSubviews];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame];
    if (self) {
        self.style = style;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews{
    self.isNeedConfigData = YES;
    [self addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

#pragma mark - Registe Detail View
- (void)registetDetailView:(NSString *)className identifier:(NSString *)identifier {
    [super registetDetailView:className identifier:identifier];
    [self.tableview registerClass:MSBaseShowCell.class forCellReuseIdentifier:identifier];
}

#pragma mark -UITableViewDelegate and DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id cellData = [self cellData:indexPath];
    if (!cellData) {
        if (self.dataArray.count > indexPath.row) {
            cellData = self.dataArray[indexPath.row];
        }
    }
    
    NSString *identifier = [self.delegate identifier:cellData indexPath:indexPath];
    NSString *className  =  self.identifierDic[identifier];
    if (!className) {
        identifier = NSStringFromClass(MSBaseShowCell.class);
    }
    MSBaseShowCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell.detailView) {
        Class detailClass = NSClassFromString(className);
        if (detailClass) {
            cell.detailView = [[detailClass alloc] init];
        }
    }
    
    if (self.isNeedConfigData) {
        if ([cell.detailView conformsToProtocol:@protocol(MSDetailViewDelegate)]) {
            [(id<MSDetailViewDelegate>)cell.detailView configData:cellData indexPath:indexPath];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0;
}

#pragma mark - Property
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:self.style];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableview registerClass:[MSBaseShowCell class] forCellReuseIdentifier:NSStringFromClass([MSBaseShowCell class])];
    }
    return _tableview;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
